 @include "readarff.awk"

 BEGIN {AfterAllRows="tar3";
     p2a("True,4,true,4,Yes,4,yes,4,Y,4,y,4,"\
         "False,2,false,2,No,2,no,2,N,2,n,2",Effects)
 }
 function effect(x) {
     return (x in Effects ? Effects[x] : 1) + rand()*Pinch
 }
 function tar3(c,   goal, total,score,memo,lives,arity) {
     lives   = Lives   ? Lives   : 5
     arity   = Arity   ? Arity   : 5
     repeats = Repeats ? Repeats : 100
     goal = Goal ? Goal : majority(c)
     total = rows1(c,goal,score,memo)
     while(lives > 0) {
	 lives = lives - 1 + builds(c,repeats,arity,score,memo,rules,)         
 }
 function rules(c,n,arity,memo,rules0, rules1,    score) {
     while (n-- > 0)
	 rule1(arity,cdf,a,v,rule,score,cache)
     
     

 }  
 function rule1(arity,cdf,a,v,rule,score,cache,     n,r,fx,m,tag,tmp,tags) {
     m= round(rand() * arity) 
     r= length(rule) + 1
     while (m-- > 0) { 
         n   = pick(cdf)
	 a1  = a[n]
	 v1  = v[n]
	 key = keys "," a1 "," v1
	 rule[r][a1][v1] 
     }
     if (key in cache)
	 score[r] = cache[key]
     else
	 score[r] = cache[key] = effects(rule1[r])
 }
 function effects(c,rule1,  row,score) {
     for(row in Has[c]) 
	 if (match(row,rule1))
	     score += effect(Data[row,K])
     return score
 }
 function match(row,rule1) {
     for(col in rule1) # if any col fails to match 
	 if (nomatch(Data[row,col],rule1[col]))
	     return 0; # return 0
     return 1 # otherwise, we're good
 }
 function nomatch(got,any,    one) {
     if (got == Missing) return 0
     for(one in any) if (got == one) return 0
     return 1
 }
 function majority(c,    klass,tmp,out,max) {
     max = -1
     for(klass in Klass[c])
	 for(value in F[c,K,klass])
	     if ((tmp = F[c,K,klass][value]) > max) {
		 out = klass
		 max = tmp
	     }
     return out
 }
 function score0(c,goal,a,v,cdf,				\
		 allOfUs,all,col,v,us,them,b,r,a,avs,tmp) {
     allOfUs   = Fall[c,K,goal]
     all       = Fall[c,K,"_"]
     for(col in Name)
	 if (col != K) 
	     for(val in F[c,col,goal]) {
		 us   = F[c,col,goal][val]
		 them = F[c,col,"_" ][val] - us
		 b    = us/allOfUs
		 r    = them/(all - allOfUs)
		 a[++n] = col
		 v[n]   = val
		 tmp[n]  = b^2/(b+r)}
     a2cdf(tmp,cdf)
 }
 